<script lang="ts" setup>
import {computed, onBeforeMount, ref, watch, onMounted, onBeforeUnmount} from 'vue';

import {useRouter} from 'vue-router';

import {AuthenticationLoginExpiredModal} from '#/components/common-ui';
import {UserRoundPen} from '#/icons';
import {useWatermark} from '#/hooks';
import {
  BasicLayout,
} from './basic';
import {
  LockScreen,
  Notification,
  UserDropdown,
} from './widgets'
import {preferences} from '#/preferences';
import {useAccessStore, useTabbarStore, useUserStore, useAuthStore, useNoticeStore} from '#/stores';
import {openWindow} from '#/utils';

import {$t} from '#/locales';
import LoginForm from '#/views/core/authentication/login.vue';

const router = useRouter();

const {setMenuList} = useTabbarStore();
setMenuList([
  'close',
  'affix',
  'maximize',
  'reload',
  'open-in-new-window',
  'close-left',
  'close-right',
  'close-other',
  'close-all',
]);

const noticeStore = useNoticeStore();

const userStore = useUserStore();
const authStore = useAuthStore();
const accessStore = useAccessStore();
const {destroyWatermark, updateWatermark} = useWatermark();

const menus = computed(() => [
  {
    handler: () => {
      router.push({path: '/personal-center',});
    },
    icon: UserRoundPen,
    text: $t('page.personal-center.title'),
  },
]);

const avatar = computed(() => {
  return userStore.userInfo?.avatar ?? preferences.app.defaultAvatar;
});

async function handleLogout() {
  await authStore.authLogout(false);
}

function handleClickLogo() {
}

onMounted(() => {
  noticeStore.start();
});

onBeforeUnmount(() => {
  noticeStore.stop();
});

watch(
    () => preferences.app.watermark,
    async (enable) => {
      if (enable) {
        await updateWatermark({
          content: `${userStore.userInfo?.username} - ${userStore.userInfo?.nickname}`,
        });
      } else {
        destroyWatermark();
      }
    },
    {
      immediate: true,
    },
);

onBeforeMount(() => {
  if (preferences.app.watermark) {
    destroyWatermark();
  }
});
</script>

<template>
  <BasicLayout
      @clear-preferences-and-logout="handleLogout"
      @click-logo="handleClickLogo"
  >
    <template #user-dropdown>
      <UserDropdown
          :avatar
          :menus
          :text="userStore.userInfo?.nickname"
          :tag-text="userStore.userInfo?.username"
          :description="userStore.userInfo?.email"
          @logout="handleLogout"
      />
    </template>
    <template #notification>
      <Notification
          :dot="noticeStore.showDot"
          :notifications="noticeStore.myselfNoticeList"
          @read="noticeStore.setRead"
          @clear="noticeStore.clearAll"
          @make-all="noticeStore.setAllRead"
      />
    </template>
    <template #extra>
      <AuthenticationLoginExpiredModal
          v-model:open="accessStore.loginExpired"
          :avatar
      >
        <LoginForm/>
      </AuthenticationLoginExpiredModal>
    </template>
    <template #lock-screen>
      <LockScreen :avatar @to-login="handleLogout"/>
    </template>
  </BasicLayout>
</template>
